<template>
	<view>
		<view>
			请输入用户名
			<input type="text" value="" v-model="username" />
		</view>
		<view>
			请输入密码
			<input type="text" value="" v-model="password" />
		</view>
		<button type="default" @tap="login">登录</button>
	</view>
</template>

<script>
	import {
		doLogin,
		getMemberInfo
	} from "@/api/user/login/index.js"
	import WxValidate  from "@/util/validate.js"
	export default {
		data() {
			return {
				username:"17596496508",
				password:"123456"
			};
		},
		onLoad() {
			this.initValidate();
		},
		methods:{
			initValidate() {
			    let rules = {
			      username:{
					  required:true,
					  tel:true
				  }
			    }
			
			    let message = {
			     username:{
					 required:"请输入用户名",
					 tel:"手机号格式不正确"
				 }
			    }
			    //实例化当前的验证规则和提示消息
			    this.WxValidate = new WxValidate(rules, message);
			  },
			async login(){
				var obj = {
					username:this.username
				}
				if (!this.WxValidate.checkForm(obj)) {
				  //表单元素验证不通过，此处给出相应提示
				    let error = this.WxValidate.errorList[0];
				   console.log(error);
					   uni.showToast({
						title:error.msg,
						icon:"none"
					   })
					   
					  return 
				  }
				var res = await doLogin({
				  "password": this.password,
				  "username": this.username
				})
				uni.setStorageSync("token",res.data.token);
				var res1 = await getMemberInfo();
				uni.setStorageSync("userInfo",res1.data.userInfo);
				uni.switchTab({
						url:"../user"
					})
				
			}
		}
	}
</script>

<style lang="scss" scoped>

</style>
